//
//  OtherDetailViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-10-24.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "OtherDetailViewController.h"
#import "BaseTableView.h"
#import "ChangeNameViewController.h"
#import "ChangePwdViewController.h"

@interface OtherDetailViewController ()<UITableViewDelegate,UITableViewDataSource,changeNameDelegate,changePhoneDelegate>
{
    
    BaseTableView *detailTableView;
}
@property(retain,nonatomic)NSArray *dataArray;
@property(retain,nonatomic)NSArray *titleArray;

@end

@implementation OtherDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitleView:@"我的信息"];
    
    detailTableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-kTabBarHeight) style:UITableViewStylePlain];
    detailTableView.dataSource = self;
    detailTableView.delegate = self;
    [self.view addSubview:detailTableView];
    
    self.nameStr = self.otherModel.nickName;
    // Do any additional setup after loading the view.
}

#pragma mark tableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    
    if (section == 1) {
        return 3;

    }
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"otherDetailCell"];
    int section = indexPath.section;
    int row = indexPath.row;
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherDetailCell"];
        
        if (section == 0 && row == 0) {
            cell.backgroundColor = [UIColor clearColor];
            UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-80)/2, (100-80)/2, 80, 80)];
            headerImageView.backgroundColor = [UIColor clearColor];
            headerImageView.tag = 100;
            headerImageView.layer.cornerRadius = 40;
            headerImageView.contentMode = UIViewContentModeScaleAspectFill;
            headerImageView.clipsToBounds = YES;
            [cell.contentView addSubview:headerImageView];
        }
        
        if (section == 1 && row == 0) {
            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 120, cell.height/2-11, 90, 30)];
            label.tag = 101;
            label.textAlignment = NSTextAlignmentRight;
            label.font = [UIFont systemFontOfSize:13.0f];
            label.textColor = MAIN_ORANGE_COLOR;
            [cell.contentView addSubview:label];
            
        }
        
        if (section == 1 && row == 1) {
            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 120, cell.height/2-11, 90, 30)];
            label.tag = 102;
            label.font = [UIFont systemFontOfSize:13.0f];
            label.textColor = MAIN_ORANGE_COLOR;
            [cell.contentView addSubview:label];
            label.textAlignment = NSTextAlignmentRight;
        }
        if (section == 1 && row == 2) {
            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 120, cell.height/2-11, 90, 30)];
            label.tag = 103;
            label.font = [UIFont systemFontOfSize:13.0f];
            label.textColor = [UIColor blueColor];
            [cell.contentView addSubview:label];
            label.textAlignment =NSTextAlignmentRight;
        }
    }
    
    switch (indexPath.section) {
        case 0:
            
        {
            UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:100];
            
            NSURL *urlStr = [NSURL URLWithString:self.otherModel.userIcon];
            [imageView setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"carema_select"]];
        }
            break;
        case 1:
            if (indexPath.row == 0) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"姓名";
                UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];
                
                if ([self.nameStr isEqual:@"(null)"]) {
                    label.text =@"未知名";
                }else{
                    label.text =self.nameStr;
                }
                
            }else if (indexPath.row == 1){
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"手机";
                UILabel *label = (UILabel *)[cell.contentView viewWithTag:102];
                
                NSString *phoneNum = [RegisterHelper shareHelper].registerPhoneNum;
                phoneNum = [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3, 5) withString:@"*****"];
                label.text = phoneNum;
                
            }else if (indexPath.row == 2){
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"修改密码";
            }
            default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1 && indexPath.row == 0) {
        
        ChangeNameViewController *changeNameVC = [[NSBundle mainBundle]loadNibNamed:@"ChangeNameViewController" owner:self options:nil][0];
        changeNameVC.delegate =self;
        changeNameVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:changeNameVC animated:YES];
        
    }else if(indexPath.section==1 && indexPath.row == 2){
        
        ChangePwdViewController *changePwdVC =[[NSBundle mainBundle]loadNibNamed:@"ChangePwdViewController" owner:self options:nil][0];
        changePwdVC.delegate = self;
        changePwdVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:changePwdVC animated:YES];

    }
}


-(void)changeName:(NSString *)phone oldPwd:(NSString *)oldPwd newsPwd:(NSString *)newsPwd{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    SetValueForKey(params, phone, @"tel");
    SetValueForKey(params, [oldPwd md5], @"psd");
    SetValueForKey(params, [newsPwd md5], @"newPsd");
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kupdatePwd
                             andHttpMethod:@"POST"
                             andParameters:params
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                         }
                         
                     }];

    
}

#pragma mark ChangeNameViewController
-(void)changeName:(NSString *)name{
    
    self.nameStr = name;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    SetValueForKey(params, [RegisterHelper shareHelper].userId, @"userId");
    SetValueForKey(params, name, @"nickName");
    
    
    SetValueForKey(params, [RegisterHelper shareHelper].userId, @"userId");
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kUpdateName
                             andHttpMethod:@"POST"
                             andParameters:params
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             [detailTableView reloadData];
                         }
                         
                     }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        return 100;
    }
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 30;
    }
    return 5;
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NSString *title;
    
    if (section == 0) {
        title = @"账号信息";
    }
       return title;
}




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
